//
//  MineYanzhengViewController.m
//  Tongcheng
//
//  Created by 邢广范 on 2017/4/9.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//

#import "MineYanzhengViewController.h"
#import "CheckUtils.h"
#import "GainCodeOperator.h"
#import "YanzhengOperator.h"

@interface MineYanzhengViewController ()

@property (weak, nonatomic) IBOutlet UITextField *telTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
- (IBAction)codeBtnClick:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
- (IBAction)sureBtnClick:(id)sender;

@end

@implementation MineYanzhengViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"手机验证";
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取验证码
- (IBAction)codeBtnClick:(id)sender {
    [self.navigationController.view endEditing:YES];
    if([CheckUtils checkPhone:self.telTextField.text]!=nil){
        [self showToastMessage:[CheckUtils checkPhone:self.telTextField.text]];
        return;
    }else{
        NSMutableDictionary* dic = [NSMutableDictionary dictionary];
        dic[@"token"] = self.config.token;
        dic[@"user_id"] = self.config.userId;
        dic[@"Mobile"] = self.telTextField.text;
        GainCodeOperator *operator = [[GainCodeOperator alloc] initWithParams:dic];
        [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
            if ([baseModel.code isEqualToString:@"1"]) {
                {
                    __block int timeout=60; //倒计时时间
                    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
                    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
                    dispatch_source_set_event_handler(_timer, ^{
                        if(timeout<=0){ //倒计时结束，关闭
                            dispatch_source_cancel(_timer);
                            dispatch_async(dispatch_get_main_queue(), ^{
                                //设置界面的按钮显示 根据自己需求设置
                                [self.codeBtn setTitle:@" 重新发送 " forState:UIControlStateNormal];
                                self.codeBtn.userInteractionEnabled = YES;
                                self.codeBtn.backgroundColor = RGB(0xF95880);
                                [self.codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                            });
                        }else{
                            int seconds = timeout % 60;
                            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                            if([strTime isEqualToString:@"00"] && timeout == 60){
                                strTime = @"60";
                            }
                            dispatch_async(dispatch_get_main_queue(), ^{
                                
                                
                                [self.codeBtn setTitle:[NSString stringWithFormat:@" %@秒重新发送 ",strTime] forState:UIControlStateNormal] ;
                                
                                self.codeBtn.userInteractionEnabled = NO;
                                self.codeBtn.backgroundColor = RGB(0x757575);
                                [self.codeBtn setTitleColor:RGB(0xCDCDCD) forState:UIControlStateNormal];
                                
                            });
                            timeout--;
                        }
                    });
                    dispatch_resume(_timer);
                    [self showToastMessage:baseModel.msg];
                }
            }
        } failBlock:^(BaseModel *baseModel) {
            
        }];
    }
}

//立即验证
- (IBAction)sureBtnClick:(id)sender {
    NSMutableDictionary* dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Mobile"] = self.telTextField.text;
    dic[@"Code"] = self.codeTextField.text;
    YanzhengOperator *operator = [[YanzhengOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [self showToastMessage:baseModel.msg];
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
